package com.xp.interview.dao.impl;

import com.xp.interview.dao.NoteModelDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

/**
 * Created by xp on 15/10/15.
 */
@Repository
public class NoteModelDaoImpl implements NoteModelDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void removeNote(String id) {
        Criteria criteria = Criteria.where("_id").is(id);
        Query query = new Query(criteria);
        mongoTemplate.remove(query,"notes");
    }

    public void updateNote(String id, String title, String content) {
        Criteria criteria = Criteria.where("_id").is(id);
        Query query = new Query(criteria);
        Update update = new Update();
        update.set("title",title);
        update.set("content",content);
        mongoTemplate.updateFirst(query,update,"notes");

    }
}
